﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace card
{
    public partial class STXXFK : Form
    {
        public STXXFK()
        {
            InitializeComponent();
        }
        public static WebDG.Service ws = new card.WebDG.Service();
        private void button2_Click(object sender, EventArgs e)
        {
            load_data();
        }
        private void load_data()
        {
            String card_add = System.Configuration.ConfigurationSettings.AppSettings["card_address"];
            //Begin time value
            string date_time_begin = this.dateTimePicker1.Value.ToString();
            int ind = date_time_begin.IndexOf(" ");
            string date_value_begin = date_time_begin.Substring(0, ind);
            //End time value
            string date_time_end = this.dateTimePicker2.Value.ToString();
            int ind2 = date_time_end.IndexOf(" ");
            string date_value_end = date_time_end.Substring(0, ind2);

            //string sql = "select * from TRUCK_LOGIN where TRUCKSTATUS=0 and ioyardid=0";
            //string sql = "select logintime as 登记时间,TRUCKNO as 车号,SHIPPER_NAME as 货主代理,"+
            //    " SHIPNAME as 船名,BILL_NO as 提单号,cargoname as 货名,pieces as 件数,IOYARDID,TRUCKSTATUS,ID from TRUCK_CARGO_LOGIN" +
            //    " where TRUCKSTATUS=1 and ioyardid=0 and trunc(logintime)<=to_date('" + date_value_end + "','yyyy-mm-dd') and trunc(logintime)>=to_date('" + date_value_begin + "','yyyy-mm-dd')  order by logintime desc";

            string sql = "select logintime as 登记时间,truckno as  车号,QDCARDNUM as 卡号,TRUCKSTATUS,id as 编号  from TRUCK_CARGO_LOGIN" +
                " where TRUCKSTATUS=1 and ioyardid=0 and trunc(logintime)<=to_date('" + date_value_end + "','yyyy-mm-dd') and trunc(logintime)>=to_date('" + date_value_begin + "','yyyy-mm-dd')" +
                " and cardadd = '" + card_add + "'group by logintime,truckno,QDCARDNUM,TRUCKSTATUS,id  order by logintime desc";
            //dt = db.ExecuteDataSet(db.GetSqlStringCommand(sql.ToString())).Tables[0];
            DataSet ds = ws.getDataSet_Serv(sql);

            this.dataGridView1.AutoGenerateColumns = true;
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.DataSource = ds.Tables[0];

            //设置某些字段不可见
            //this.dataGridView1.Columns["IOYARDID"].Visible = false;
            this.dataGridView1.Columns["TRUCKSTATUS"].Visible = false;
            //this.dataGridView1.Columns["ID"].Visible = false;
            //设置checkbox 列的宽度
            this.dataGridView1.Columns["SelectFlag"].Width = 50;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                object obj = this.dataGridView1.Rows[i].Cells[0].Value;
                string select = obj == null ? "" : obj.ToString();
                if (select == "True")
                {
                    string login_time = this.dataGridView1.Rows[i].Cells["登记时间"].Value.ToString();
                    string truck_no = this.dataGridView1.Rows[i].Cells["车号"].Value.ToString();
                    string op_user = Session.Operator_User.ToString();
                    //string update_sql = "update TRUCK_CARGO_LOGIN set TRUCKSTATUS=2,CARDTIME=sysdate where ID='" + id + "'";
                    String update_sql = "update TRUCK_CARGO_LOGIN set TRUCKSTATUS=2,CARDTIME=sysdate where logintime=to_date('" + login_time + "','yyyy-mm-dd hh24:mi:ss') and TRUCKNO='" + truck_no + "' and TRUCKSTATUS<>'9'";
                    string new_id = create_id();
                    string insert_sql = "insert into TRUCK_LOG(ID,TRUCKNO,OPERATENAME,XGSJ,XGNR) values(" + new_id +
                        ",'" + truck_no + "','" + op_user + "',sysdate,'停车场放车确认')";
                    int ret1 = ws.execute_UID_Serv(update_sql);
                    int ret2 = ws.execute_UID_Serv(insert_sql);

                    //打印
                    string strid = this.dataGridView1.Rows[i].Cells["编号"].Value.ToString();
                    PrintCode pc = new PrintCode();
                    pc.Run(strid, "大港公司市提车辆登记小票");

                }
            }
            load_data();
            MessageBox.Show("操作成功!");
        }
        //Create ID
        private string create_id()
        {
            string sequence = "";
            string sqlStr = "select SEQ_TRUCK.nextval from dual";
            DataSet ds1 = ws.getDataSet_Serv(sqlStr);
            if (ds1.Tables[0].Rows.Count > 0)
            {
                sequence = ds1.Tables[0].Rows[0][0].ToString();
            }
            string year_str = DateTime.Now.Year.ToString();
            string month_str = DateTime.Now.Month.ToString();
            string day_str = DateTime.Now.Day.ToString();
            string hour_str = DateTime.Now.Hour.ToString();
            string minute_str = DateTime.Now.Minute.ToString();
            string second_str = DateTime.Now.Second.ToString();
            string id = year_str + month_str + day_str + hour_str + minute_str + second_str + sequence;
            return id;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
        {
            //e.ToolTipText = "姓名：" + persons[e.RowIndex].Name + " 性别：" + persons[e.RowIndex].Sex;
            if (e.RowIndex>0)
            {
                string car_no = this.dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
                string sql = "select CARGONAME,SHIPNAME from TRUCK_CARGO_LOGIN where TRUCKNO ='" + car_no + "'";
                DataSet ds1 = ws.getDataSet_Serv(sql);
                string tooltip = "";
                if (ds1.Tables[0].Rows.Count > 0)
                {
                    
                    for (int i = 0; i < ds1.Tables[0].Rows.Count;i++)
                    {
                        tooltip = tooltip + "船名: " + ds1.Tables[0].Rows[i]["SHIPNAME"].ToString() + " 货名: " + ds1.Tables[0].Rows[i]["CARGONAME"].ToString() + "\r\n";
                    }
                }
                e.ToolTipText = tooltip;
            }
            
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
            if (this.dataGridView1.CurrentCell.ColumnIndex == 0)
            {
                //获取DataGridView中CheckBox的Cell
                DataGridViewCheckBoxCell dgvCheck = (DataGridViewCheckBoxCell)(this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[0]);

                //获取被选中列的相关信息
                //Double dAdd = double.Parse(this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[5].Value.ToString());

               //根据单击时，Cell的值进行处理。EditedFormattedValue和Value均可以
               //若单击时，CheckBox没有被勾上
                if (Convert.ToBoolean(dgvCheck.EditedFormattedValue) == false)
                {
                    //通过程序完成CheckBox是否勾上的控制
                    dgvCheck.Value = true;
                }

               //若单击时，CheckBox已经被勾上
                else
                {
                    dgvCheck.Value = false;
                } 
            }

            set_detail_grid();
        }

        private void set_detail_grid()
        {
            string where_str = "";
            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                object obj = this.dataGridView1.Rows[i].Cells[0].Value;
                string select = obj == null ? "" : obj.ToString();
                if (select == "True")
                {
                    string login_time = this.dataGridView1.Rows[i].Cells["登记时间"].Value.ToString();
                    string truck_no = this.dataGridView1.Rows[i].Cells["车号"].Value.ToString();
                    where_str = where_str + "(TRUCKNO='" + truck_no + "' and logintime = to_date('" + login_time +"','yyyy-mm-dd hh24:mi:ss')"+ ") or ";
                }
            }
            if (where_str.Length > 0)
            {
                where_str = "where " + where_str;
                where_str = where_str.Substring(0, where_str.Length - 3);


            }
            else
            {
                where_str = "where TRUCKNO=''";
            }
            string sql = "select LOGINTIME as 登记时间,TRUCKNO as 车号,CARGONAME as 货名,SHIPPER_NAME as 货主代理,SHIPNAME as 船名,BILL_NO as 合同号 from TRUCK_CARGO_LOGIN " + where_str + " order by logintime desc";
            DataSet ds = ws.getDataSet_Serv(sql);

            this.dataGridView2.AutoGenerateColumns = true;
            this.dataGridView2.AllowUserToAddRows = false;
            this.dataGridView2.DataSource = ds.Tables[0];
        }

        private void button_all_select_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                DataGridViewCheckBoxCell dgvCheck = (DataGridViewCheckBoxCell)(this.dataGridView1.Rows[i].Cells[0]);
                dgvCheck.Value = true;
            }
        }

    }
}
